package org.jz.gae.redbook.spb.client;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;

public class AppController implements ValueChangeHandler<String> 
{
  private final HandlerManager eventBus;
  private final RedBookServiceAsync rpcService; 
  private HasWidgets container;
//  private FamilyEvents familyEvents = new FamilyEvents();
//  private LocationEvents locationEvents = new LocationEvents();
//  private DistrictEvents districtEvents = new DistrictEvents();
  
  public AppController(RedBookServiceAsync rpcService, HandlerManager eventBus) 
  {
    this.eventBus = eventBus;
    this.rpcService = rpcService;
    bind();
  }
  
  private void bind() {
    History.addValueChangeHandler(this);

//    familyEvents.bind(eventBus, rpcService);
//    locationEvents.bind(eventBus, rpcService);
//    districtEvents.bind(eventBus, rpcService);
    
  }
  
   public void go(final HasWidgets container) 
  {
    this.container = container;
 //   familyEvents.setContainer(container);
 //   locationEvents.setContainer(container);
 //   districtEvents.setContainer(container);
    
    if ("".equals(History.getToken())) 
    {
        History.newItem("welcome");
    }
    else 
    {
        System.err.println("go : -");  
        History.fireCurrentHistoryState();
    }
  }

  public void onValueChange(ValueChangeEvent<String> event) 
  {
    String token = event.getValue();
    
    if (token != null) 
    {
//      Presenter presenter = null;

      System.err.println("token : " + token);  

 //     if (token.equals("welcome"))
 //     {
 //   	  presenter = new WelcomePresenter(rpcService, eventBus, new WelcomeView());
 //     }
 //     if (presenter == null)
 //     {
 //         presenter = familyEvents.performToken(token);
 //     }
 //     if (presenter == null)
 //     {
 //         presenter = locationEvents.performToken(token);
 //     }
 //     if (presenter == null)
 //     {
 //         presenter = districtEvents.performToken(token);
 //    }
      //else if (token.equals("family-edit")) 
      //{
      //    presenter = new FamilyEditPresenter(rpcService, eventBus, new FamilyEditView());
      //}
      
 //     if (presenter != null) {
 //       presenter.go(container);
      }
    }
  } 

